// src/composables/usePluginData.ts
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import {
  getDataStatistics,
  getBatches,
  getDataItems,
  getDataItemDetail,
  reprocessDataItem,
  deleteDataItem
} from '@/api/pluginData'
import type { PluginDataBatch, PluginDataItem } from '@/api/pluginData'

// 状态管理
const statistics = ref({
  totalBatches: 0,
  totalRecords: 0,
  todayBatches: 0,
  todayRecords: 0,
  batchByStatus: [] as Array<{status: string, count: number}>,
  dataByCategory: [] as Array<{category: string, count: number}>
})

const batches = ref<PluginDataBatch[]>([])
const dataItems = ref<PluginDataItem[]>([])

const batchPagination = reactive({
  currentPage: 1,
  pageSize: 20,
  total: 0
})

const itemPagination = reactive({
  currentPage: 1,
  pageSize: 20,
  total: 0
})

const loading = ref({
  statistics: false,
  batches: false,
  items: false,
  detail: false
})

// 获取数据统计信息
const fetchStatistics = async () => {
  try {
    loading.value.statistics = true
    const result = await getDataStatistics()
    
    // 根据后端实际返回的数据结构进行处理
    const data = result.data
    
    // 处理统计数据，确保正确的数据类型
    statistics.value = {
      totalBatches: typeof data.totalBatches === 'number' ? data.totalBatches : 0,
      totalRecords: typeof data.totalRecords === 'number' ? data.totalRecords : 0,
      todayBatches: typeof data.todayBatches === 'number' ? data.todayBatches : 0,
      todayRecords: typeof data.todayRecords === 'number' ? data.todayRecords : 0,
      batchByStatus: Array.isArray(data.batchByStatus) ? data.batchByStatus : [],
      dataByCategory: Array.isArray(data.dataByCategory) ? data.dataByCategory : []
    }
  } catch (error: any) {
    ElMessage.error('获取统计信息失败: ' + error.message)
  } finally {
    loading.value.statistics = false
  }
}

// 获取批次列表
const fetchBatches = async (params: { page?: number; pageSize?: number; status?: string } = {}) => {
  try {
    loading.value.batches = true
    const page = params.page || batchPagination.currentPage
    const pageSize = params.pageSize || batchPagination.pageSize
    
    const result = await getBatches({
      page,
      pageSize,
      status: params.status
    })
    
    batches.value = result.data.list
    batchPagination.total = result.data.pagination.total
    batchPagination.currentPage = result.data.pagination.page
    batchPagination.pageSize = result.data.pagination.pageSize
  } catch (error: any) {
    ElMessage.error('获取批次列表失败: ' + error.message)
  } finally {
    loading.value.batches = false
  }
}

// 获取数据项列表
const fetchDataItems = async (params: { 
  page?: number; 
  pageSize?: number; 
  batchId?: string; 
  category?: string; 
  status?: string 
} = {}) => {
  try {
    loading.value.items = true
    const page = params.page || itemPagination.currentPage
    const pageSize = params.pageSize || itemPagination.pageSize
    
    const result = await getDataItems({
      page,
      pageSize,
      batchId: params.batchId,
      category: params.category,
      status: params.status
    })
    
    dataItems.value = result.data.list
    itemPagination.total = result.data.pagination.total
    itemPagination.currentPage = result.data.pagination.page
    itemPagination.pageSize = result.data.pagination.pageSize
  } catch (error: any) {
    ElMessage.error('获取数据项列表失败: ' + error.message)
  } finally {
    loading.value.items = false
  }
}

// 获取数据项详情
const fetchDataItemDetail = async (itemId: string) => {
  try {
    loading.value.detail = true
    const result = await getDataItemDetail(itemId)
    return result.data
  } catch (error: any) {
    ElMessage.error('获取数据项详情失败: ' + error.message)
    return null
  } finally {
    loading.value.detail = false
  }
}

// 重新处理数据项
const reprocessItem = async (itemId: string) => {
  try {
    const result = await reprocessDataItem(itemId)
    if (result.data.code === 0) {
      ElMessage.success('重新处理成功')
      return true
    } else {
      ElMessage.error('重新处理失败: ' + result.data.message)
      return false
    }
  } catch (error: any) {
    ElMessage.error('重新处理失败: ' + error.message)
    return false
  }
}

// 删除数据项
const deleteItem = async (itemId: string) => {
  try {
    const result = await deleteDataItem(itemId)
    if (result.data.code === 0) {
      ElMessage.success('删除成功')
      return true
    } else {
      ElMessage.error('删除失败: ' + result.data.message)
      return false
    }
  } catch (error: any) {
    ElMessage.error('删除失败: ' + error.message)
    return false
  }
}

// 导出所有功能
export function usePluginData() {
  return {
    // 状态
    statistics,
    batches,
    dataItems,
    batchPagination,
    itemPagination,
    loading,
    
    // 方法
    fetchStatistics,
    fetchBatches,
    fetchDataItems,
    fetchDataItemDetail,
    reprocessItem,
    deleteItem
  }
}